package game.module.sss.logic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.RoomServer;
import game.common.PrimitiveTypeConstants;
import game.entity.PlayingRole;
import game.lan.msg.SssStart2HsMessage;
import game.module.RoomType;
import game.module.ddz.bean.DdzLanPlayerInfo;
import game.module.sss.SssConstants;
import game.module.sss.bean.SssRound;
import game.module.user.dao.LanSessionCache;
import game.session.SessionManager;
import io.netty.channel.Channel;
import lion.common.ArrayUtility;
import lion.math.RandomDispatcher;
import lua.LuaMessageSss.IntList;
import lua.LuaMessageSss.PushSssCalculate;
import lua.LuaMessageSss.PushSssNextRound;
import lua.LuaMessageSss.SssCardArray;

public class SssLogic {

	private static Logger logger = LoggerFactory.getLogger(SssLogic.class);

	static class SingletonHolder {
		static SssLogic instance = new SssLogic();
	}

	public static SssLogic getInstance() {
		return SingletonHolder.instance;
	}

	public void sendSssCalculateFangka(short fee, byte type, List<Integer> players) {
		String rsLanHost = RoomServer.getInstance().getServerConfig().getHallLanhost();
		int rsLanPort = RoomServer.getInstance().getServerConfig().getHallLanport();
		boolean reconnectRet = RoomServer.getInstance().getLanClientManager().connect(rsLanHost, rsLanPort);
		if (reconnectRet) {
			RoomServer.getInstance().getLanClientManager().sendCalculateFangKa(rsLanHost, rsLanPort, fee, type,
					players);
		} else {
			logger.error("connect 2 roomServer error!rsLanHost={},rsLanPort={}", rsLanHost, rsLanPort);
		}
	}

	public void generateCards(Channel channel, String roomId, short roundCount, byte antiCheet, byte mapai,
			byte daqiang, byte sunzi_a, byte baodao, byte is_jindou, int jindoudi, byte feeType, int fee,
			List<DdzLanPlayerInfo> playerList, byte player_count, byte[] add_color) {
		logger.info(
				"sss generate cards,playerList={},roomId={},roundCount={},antiCheet={},mapai={},daqiang={},sunzi_a={},baodao={},is_jindou={},jindou_type={},player_count={},add_color={}",
				playerList, roomId, roundCount, antiCheet, mapai, daqiang, sunzi_a, baodao, is_jindou, jindoudi,player_count,add_color);
		int playerCount = playerList.size();
		SssRound sssRound = new SssRound();
		List<byte[]> cardList = randCards(playerCount,add_color);
		sssRound.setCards(cardList);
		sssRound.setRoomUuid(roomId);
		// 房间参数
		sssRound.setVotediss(new boolean[playerList.size()]);
		sssRound.setRoundCount(roundCount);
		sssRound.setAntiCheet(antiCheet);
		sssRound.setMapai(mapai);
		sssRound.setDaqiang(daqiang);
		sssRound.setSunzi_a(sunzi_a);
		sssRound.setBaodao(baodao);
		sssRound.setIs_jindou(is_jindou);
		sssRound.setJindouDi(jindoudi);
		// sssRound.setJindou_type((byte) jindoudi);
		sssRound.setRoomFee(fee);
		sssRound.setRoomFeeType(feeType);
		sssRound.setPlayer_count(player_count);
		sssRound.setAdd_color(add_color);
		// 初始化
		sssRound.setCurrentRound((short) 1);
		sssRound.setSssAlgorithm(new SssAlgorithm());
		// init params
		sssRound.m_bSpecialTypeTable = new boolean[playerCount];
		sssRound.m_bToltaSocre = new int[playerCount];
		byte[][][] m_bSegmentCard = new byte[playerCount][3][SssConstants.MAX_COUNT];
		sssRound.setM_bSegmentCard(m_bSegmentCard);
		sssRound.m_lCellScore = 10;
		SssStart2HsMessage respMsg = new SssStart2HsMessage(channel.alloc());
		respMsg.writeString(roomId);
		respMsg.writeInt(playerCount);
		for (int i = 0; i < playerCount; i++) {
			int playerId = playerList.get(i).getPlayerId();
			byte[] aPlayerCards = sssRound.getCards().get(i);
			respMsg.writeInt(playerId);
			respMsg.writeInt(aPlayerCards.length);
			for (byte aCard : aPlayerCards) {
				respMsg.writeByte(aCard);
			}
		}
		// 缓存玩家信息
		List<Integer> playerIds = new ArrayList<>();
		sssRound.setPlayerIds(playerIds);
		for (DdzLanPlayerInfo ddzLanPlayerInfo : playerList) {
			LanSessionCache.getInstance().addSession(ddzLanPlayerInfo.getSessionId(), ddzLanPlayerInfo.getPlayerId());
			playerIds.add(ddzLanPlayerInfo.getPlayerId());
		}
		// 缓存牌局信息
		for (DdzLanPlayerInfo ddzLanPlayerInfo : playerList) {
			SssDataManager.getInstance().addRoundCache(ddzLanPlayerInfo.getPlayerId(), sssRound);
		}
		long create_time = System.currentTimeMillis();
		sssRound.setCreate_time(create_time);
		channel.writeAndFlush(respMsg);
		if(sssRound.getIs_jindou()==1) {//金豆房扣钱
			sendSssJindouRoomChargeFee(sssRound.getJindouDi(),sssRound.getPlayerIds());
		}
	}

	/**
	 * 计算倒数
	 * 
	 * @param sssRound
	 */
	private void ComputeChout(SssRound sssRound) {
		byte[][] cbResult = new byte[sssRound.getPlayerIds().size()][3];
		sssRound.setCbResult(cbResult);
		byte[] cbSpecialResult = new byte[sssRound.getPlayerIds().size()];
		sssRound.setCbSpecialResult(cbSpecialResult);
		boolean[] m_bSpecialTypeTable = sssRound.m_bSpecialTypeTable;
		SssAlgorithm m_GameLogic = sssRound.getSssAlgorithm();
		byte[][][] m_bSegmentCard = sssRound.getM_bSegmentCard();
		List<byte[]> m_bUserCardData = sssRound.getCards();
		for (int wUser = 0; wUser < sssRound.getPlayerIds().size(); wUser++) {
			// 牌比道数
			if (m_bSpecialTypeTable[wUser] == false) {
				//后敦5同
				if (cbResult[wUser][2] == (byte) 0 && (SssConstants.CT_FIVE_SAME == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][2] = 10;
				}
				//中敦5同
				if (cbResult[wUser][1] == (byte) 0 && (SssConstants.CT_FIVE_SAME == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][1] = 20;
				}
				// 后敦同花顺
				if (cbResult[wUser][2] == (byte) 0 && (SssConstants.CT_FIVE_STRAIGHT_FLUSH_FIRST_A == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData)
						|| SssConstants.CT_FIVE_STRAIGHT_FLUSH_NO_A == m_GameLogic.GetCardType(m_bSegmentCard[wUser][2],
								(byte) 5, m_GameLogic.btCardSpecialData)
						|| SssConstants.CT_FIVE_STRAIGHT_FLUSH_BACK_A == m_GameLogic
								.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][2] = 5;
				}
				// 中敦同花顺
				if (cbResult[wUser][1] == (byte) 0 && (SssConstants.CT_FIVE_STRAIGHT_FLUSH_FIRST_A == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData)
						|| SssConstants.CT_FIVE_STRAIGHT_FLUSH_NO_A == m_GameLogic.GetCardType(m_bSegmentCard[wUser][1],
								(byte) 5, m_GameLogic.btCardSpecialData)
						|| SssConstants.CT_FIVE_STRAIGHT_FLUSH_BACK_A == m_GameLogic
								.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][1] = 10;
				}
				// 后敦炸弹
				if (cbResult[wUser][2] == (byte) 0 && (SssConstants.CT_FIVE_FOUR_ONE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][2] = 4;
				}
				// 中敦炸弹
				if (cbResult[wUser][1] == (byte) 0 && (SssConstants.CT_FIVE_FOUR_ONE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][1] = 8;
				}
				// 后敦葫芦
				if (cbResult[wUser][2] == (byte) 0 && (SssConstants.CT_FIVE_THREE_DEOUBLE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][2] = 1;
				}
				// 中敦葫芦
				if (cbResult[wUser][1] == (byte) 0 && (SssConstants.CT_FIVE_THREE_DEOUBLE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][1] = 2;
				}

				// 后墩同花
				if (cbResult[wUser][2] == (byte) 0 && SssConstants.CT_FIVE_FLUSH == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData)) {
					cbResult[wUser][2] = 1;
				}
				// 中墩同花
				if (cbResult[wUser][1] == (byte) 0 && SssConstants.CT_FIVE_FLUSH == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData)) {
					cbResult[wUser][1] = 1;
				}
				// 后墩顺子
				if (cbResult[wUser][2] == (byte) 0 && (SssConstants.CT_FIVE_MIXED_FLUSH_NO_A == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData)
						|| SssConstants.CT_FIVE_MIXED_FLUSH_FIRST_A == m_GameLogic.GetCardType(m_bSegmentCard[wUser][2],
								(byte) 5, m_GameLogic.btCardSpecialData)
						|| SssConstants.CT_FIVE_MIXED_FLUSH_BACK_A == m_GameLogic.GetCardType(m_bSegmentCard[wUser][2],
								(byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][2] = 1;
				}
				// 中墩顺子
				if (cbResult[wUser][1] == (byte) 0 && (SssConstants.CT_FIVE_MIXED_FLUSH_NO_A == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData)
						|| SssConstants.CT_FIVE_MIXED_FLUSH_FIRST_A == m_GameLogic.GetCardType(m_bSegmentCard[wUser][1],
								(byte) 5, m_GameLogic.btCardSpecialData)
						|| SssConstants.CT_FIVE_MIXED_FLUSH_BACK_A == m_GameLogic.GetCardType(m_bSegmentCard[wUser][1],
								(byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][1] = 1;
				}
				// 后敦三张
				if (cbResult[wUser][2] == (byte) 0 && (SssConstants.CT_THREE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][2] = 1;
				}
				// 中敦三张
				if (cbResult[wUser][1] == (byte) 0 && (SssConstants.CT_THREE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][1] = 1;
				}
				// 前敦三张
				if (cbResult[wUser][0] == (byte) 0 && (SssConstants.CT_THREE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][0], (byte) 3, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][0] = 3;
				}
				// 后敦两对
				if (cbResult[wUser][2] == (byte) 0 && (SssConstants.CT_FIVE_TWO_DOUBLE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][2] = 1;
				}
				// 中敦两对
				if (cbResult[wUser][1] == (byte) 0 && (SssConstants.CT_FIVE_TWO_DOUBLE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][1] = 1;
				}
				// 后敦一对
				if (cbResult[wUser][2] == (byte) 0 && (SssConstants.CT_ONE_DOUBLE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][2] = 1;
				}
				// 中敦一对
				if (cbResult[wUser][1] == (byte) 0 && (SssConstants.CT_ONE_DOUBLE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][1] = 1;
				}
				// 前敦一对
				if (cbResult[wUser][0] == (byte) 0 && (SssConstants.CT_ONE_DOUBLE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][0], (byte) 3, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][0] = 1;
				}
				// 后敦散牌
				if (cbResult[wUser][2] == (byte) 0 && (SssConstants.CT_SINGLE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][2], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][2] = 1;
				}
				// 中敦散牌
				if (cbResult[wUser][1] == (byte) 0 && (SssConstants.CT_SINGLE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][1], (byte) 5, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][1] = 1;
				}
				// 前敦散牌
				if (cbResult[wUser][0] == (byte) 0 && (SssConstants.CT_SINGLE == m_GameLogic
						.GetCardType(m_bSegmentCard[wUser][0], (byte) 3, m_GameLogic.btCardSpecialData))) {
					cbResult[wUser][0] = 1;
				}
			} else {
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_THIRTEEN_FLUSH == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					cbSpecialResult[wUser] = 108;
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_THIRTEEN == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					cbSpecialResult[wUser] = 36;
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_TWELVE_KING == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					cbSpecialResult[wUser] = 24;
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_THREE_STRAIGHTFLUSH == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					cbSpecialResult[wUser] = 20;
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_THREE_BOMB == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					cbSpecialResult[wUser] = 20;
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_ALL_BIG == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					cbSpecialResult[wUser] = 10;
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_ALL_SMALL == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					cbSpecialResult[wUser] = 10;
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_SAME_COLOR == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					cbSpecialResult[wUser] = 10;
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_FOUR_THREESAME == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					cbSpecialResult[wUser] = 6;
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_FIVEPAIR_THREE == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					cbSpecialResult[wUser] = 5;
					// logger.info("#ComputeChout
					// SssConstants.CT_FIVEPAIR_THREE,mycard={},wUser={},specialdata={}",m_bUserCardData.get(wUser),wUser,m_GameLogic.btCardSpecialData);
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_SIXPAIR == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					// cbSpecialResult[wUser] = 4;
					cbSpecialResult[wUser] = sssRound.getBaodao();
					// logger.info("#ComputeChout SssConstants.CT_SIXPAIR,wUser={}",wUser);
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_THREE_STRAIGHT == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					// cbSpecialResult[wUser] = 4;
					cbSpecialResult[wUser] = sssRound.getBaodao();
				}
				if (cbSpecialResult[wUser] == (byte) 0 && SssConstants.CT_THREE_FLUSH == m_GameLogic
						.GetCardType(m_bUserCardData.get(wUser), (byte) 13, m_GameLogic.btCardSpecialData)) {
					// cbSpecialResult[wUser] = 3;
					cbSpecialResult[wUser] = sssRound.getBaodao();
				}
			}
		}
	}

	public void ComputeResult(SssRound sssRound) {
		int GAME_PLAYER = sssRound.getPlayerIds().size();
		// 游戏变量
		byte[][] cbResult = sssRound.getCbResult();
		byte[] cbSpecialResult = sssRound.getCbSpecialResult();
		boolean[] m_bSpecialTypeTable = sssRound.m_bSpecialTypeTable;
		SssAlgorithm m_GameLogic = sssRound.getSssAlgorithm();
		byte[][][] m_bSegmentCard = sssRound.getM_bSegmentCard();
		List<byte[]> m_bUserCardData = sssRound.getCards();

		sssRound.m_bCompareResult = new int[GAME_PLAYER][3];
		sssRound.m_bShootState = new int[6][2];
		sssRound.m_bThreeKillResult = new int[GAME_PLAYER];
		sssRound.m_bToltalWinDaoShu = new int[GAME_PLAYER];
		sssRound.m_bCompareDouble = new int[GAME_PLAYER];
		sssRound.m_bSpecialCompareResult = new int[GAME_PLAYER];
		sssRound.m_lGameScore = new long[GAME_PLAYER];
		// sssRound.m_bToltaSocre = new int[GAME_PLAYER];
		sssRound.m_nXShoot = 0;
		int[] WinNum = new int[GAME_PLAYER];
		int[] WinShuiShu = new int[] { 0, 0, 0, 0 };
		for (int i = 0; i < 6; i++) {
			sssRound.m_bShootState[i][0] = -1;
			sssRound.m_bShootState[i][1] = -1;
			if (i < GAME_PLAYER) {
				sssRound.m_bThreeKillResult[i] = 0;
			}
		}

		for (int wUser = 0; wUser < GAME_PLAYER; ++wUser) {
			long lWinDaoShu = 0;
			// 玩家判断
			// if(!m_bPlayer[wUser]||m_bUserLeft[wUser]) continue ;

			byte cbConstUserFrontDaoShu = cbResult[wUser][0];
			byte cbConstUserMidDaoShu = cbResult[wUser][1];
			byte cbConstUserBackDaoShu = cbResult[wUser][2];
			byte cbSpecialUserDaoShu = cbSpecialResult[wUser];
			// 输赢道数

			for (int wCmpUser = 0; wCmpUser < GAME_PLAYER; ++wCmpUser) {
				int nIndex = 0;

				// 玩家判断
				// if(!m_bPlayer[wCmpUser]||m_bUserLeft[wCmpUser]) continue ;

				if (wUser != wCmpUser) {
					byte cbConstCmpUserFrontDaoShu = cbResult[wCmpUser][0];
					byte cbConstCmpUserMidDaoShu = cbResult[wCmpUser][1];
					byte cbConstCmpUserBackDaoShu = cbResult[wCmpUser][2];
					byte cbSpecialCmpUserDaoShu = cbSpecialResult[wCmpUser];

					// if(m_bDragon[wUser]&&m_bDragon[wCmpUser]==false) ///<一家倒水一家不倒水
					// {
					// if(m_bSpecialTypeTable[wCmpUser]==false) ///<不等于特殊牌型
					// {
					//
					// lWinDaoShu-=cbConstCmpUserFrontDaoShu;
					// m_bCompareResult[wUser][0]-=cbConstCmpUserFrontDaoShu;
					//
					// lWinDaoShu-=cbConstCmpUserMidDaoShu;
					// m_bCompareResult[wUser][1]-=cbConstCmpUserMidDaoShu;
					//
					// lWinDaoShu-=cbConstCmpUserBackDaoShu;
					// m_bCompareResult[wUser][2]-=cbConstCmpUserBackDaoShu;
					//
					// }
					// else
					// {
					// lWinDaoShu-=cbSpecialCmpUserDaoShu;
					// m_bSpecialCompareResult[wUser]-=cbSpecialCmpUserDaoShu;
					// }
					// }
					// else if(m_bDragon[wUser]==false&&m_bDragon[wCmpUser]) ///<一家不倒水，一家倒水
					// {
					// if(m_bSpecialTypeTable[wUser]==false) ///<不等于特殊牌型
					// {
					// lWinDaoShu+=cbConstUserFrontDaoShu;
					// m_bCompareResult[wUser][0]+=cbConstUserFrontDaoShu;
					//
					// lWinDaoShu+=cbConstUserMidDaoShu;
					// m_bCompareResult[wUser][1]+=cbConstUserMidDaoShu;
					//
					// lWinDaoShu+=cbConstUserBackDaoShu;
					// m_bCompareResult[wUser][2]+=cbConstUserBackDaoShu;
					//
					// WinNum[wUser]++;
					// }
					// else
					// {
					// lWinDaoShu+=cbSpecialUserDaoShu;
					// m_bSpecialCompareResult[wUser]+=cbSpecialUserDaoShu;
					// }
					// }
					// else if(m_bDragon[wUser]==false&&m_bDragon[wCmpUser]==false)
					// {
					long aPlayerWinDao = 0;
					if (m_bSpecialTypeTable[wUser] == false && m_bSpecialTypeTable[wCmpUser] == false) {

						if (m_GameLogic.CompareCard(m_bSegmentCard[wCmpUser][0], m_bSegmentCard[wUser][0], (byte) 3,
								(byte) 3, true, sssRound.getSunzi_a())) {
							int win1 = checkMaPai(sssRound, wUser, wCmpUser) ? cbConstUserFrontDaoShu * 2
									: cbConstUserFrontDaoShu;
							aPlayerWinDao += win1;
							sssRound.m_bCompareResult[wUser][0] += win1;
						} else {
							int loose1 = checkMaPai(sssRound, wUser, wCmpUser) ? cbConstCmpUserFrontDaoShu * 2
									: cbConstCmpUserFrontDaoShu;
							aPlayerWinDao -= loose1;
							// aPlayerWinDao -= cbConstCmpUserFrontDaoShu;
							sssRound.m_bCompareResult[wUser][0] -= loose1;
						}

						if (m_GameLogic.CompareCard(m_bSegmentCard[wCmpUser][1], m_bSegmentCard[wUser][1], (byte) 5,
								(byte) 5, true, sssRound.getSunzi_a())) {
							int win2 = checkMaPai(sssRound, wUser, wCmpUser) ? cbConstUserMidDaoShu * 2
									: cbConstUserMidDaoShu;
							aPlayerWinDao += win2;
							// aPlayerWinDao += cbConstUserMidDaoShu;
							sssRound.m_bCompareResult[wUser][1] += win2;
							// sssRound.m_bCompareResult[wUser][1] += cbConstUserMidDaoShu;
						} else {
							int loose2 = checkMaPai(sssRound, wUser, wCmpUser) ? cbConstCmpUserMidDaoShu * 2
									: cbConstCmpUserMidDaoShu;
							aPlayerWinDao -= loose2;
							sssRound.m_bCompareResult[wUser][1] -= loose2;
							// aPlayerWinDao -= cbConstCmpUserMidDaoShu;
							// sssRound.m_bCompareResult[wUser][1] -= cbConstCmpUserMidDaoShu;
						}
						if (m_GameLogic.CompareCard(m_bSegmentCard[wCmpUser][2], m_bSegmentCard[wUser][2], (byte) 5,
								(byte) 5, true, sssRound.getSunzi_a())) {
							int win3 = checkMaPai(sssRound, wUser, wCmpUser) ? cbConstUserBackDaoShu * 2
									: cbConstUserBackDaoShu;
							aPlayerWinDao += win3;
							sssRound.m_bCompareResult[wUser][2] += win3;
							// aPlayerWinDao += cbConstUserBackDaoShu;
							// sssRound.m_bCompareResult[wUser][2] += cbConstUserBackDaoShu;
						} else {
							int loose3 = checkMaPai(sssRound, wUser, wCmpUser) ? cbConstCmpUserBackDaoShu * 2
									: cbConstCmpUserBackDaoShu;
							aPlayerWinDao -= loose3;
							sssRound.m_bCompareResult[wUser][2] -= loose3;
							// aPlayerWinDao -= cbConstCmpUserBackDaoShu;
							// sssRound.m_bCompareResult[wUser][2] -= cbConstCmpUserBackDaoShu;
						}
						// 打枪+3
						if (m_GameLogic.CompareCard(m_bSegmentCard[wCmpUser][0], m_bSegmentCard[wUser][0], (byte) 3,
								(byte) 3, true, sssRound.getSunzi_a())
								&& m_GameLogic.CompareCard(m_bSegmentCard[wCmpUser][1], m_bSegmentCard[wUser][1],
										(byte) 5, (byte) 5, true, sssRound.getSunzi_a())
								&& m_GameLogic.CompareCard(m_bSegmentCard[wCmpUser][2], m_bSegmentCard[wUser][2],
										(byte) 5, (byte) 5, true, sssRound.getSunzi_a())) {
							if (sssRound.getDaqiang() == 2) {
								aPlayerWinDao += 3;
								sssRound.m_bCompareDouble[wUser] += 3;
							} else {
								sssRound.m_bCompareDouble[wUser] += aPlayerWinDao;
								aPlayerWinDao *= 2;
							}

							sssRound.m_bShootState[sssRound.m_nXShoot][0] = wUser; /// <赢的
							sssRound.m_bShootState[sssRound.m_nXShoot][1] = wCmpUser; /// <输的
							sssRound.m_nXShoot++;
							WinNum[wUser]++;
						}
						// 被打枪-3
						else if (!m_GameLogic.CompareCard(m_bSegmentCard[wCmpUser][0], m_bSegmentCard[wUser][0],
								(byte) 3, (byte) 3, true, sssRound.getSunzi_a())
								&& !m_GameLogic.CompareCard(m_bSegmentCard[wCmpUser][1], m_bSegmentCard[wUser][1],
										(byte) 5, (byte) 5, true, sssRound.getSunzi_a())
								&& !m_GameLogic.CompareCard(m_bSegmentCard[wCmpUser][2], m_bSegmentCard[wUser][2],
										(byte) 5, (byte) 5, true, sssRound.getSunzi_a())) {
							if (sssRound.getDaqiang() == 2) {
								aPlayerWinDao -= 3;
								sssRound.m_bCompareDouble[wUser] -= 3;
							} else {
								sssRound.m_bCompareDouble[wUser] += aPlayerWinDao;
								aPlayerWinDao *= 2;
							}
							// aPlayerWinDao *= 2;
							// sssRound.m_bCompareDouble[wUser] *= 2;
						}
					} else if (m_bSpecialTypeTable[wUser] == true && m_bSpecialTypeTable[wCmpUser] == false) {
						aPlayerWinDao += cbSpecialUserDaoShu;
						// logger.info("lWinDaoShu={},cbSpecialUserDaoShu={},wUser={},wCmpUser={}",lWinDaoShu,cbSpecialUserDaoShu,wUser,wCmpUser);
						sssRound.m_bSpecialCompareResult[wUser] += cbSpecialUserDaoShu;
					} else if (m_bSpecialTypeTable[wUser] == true && m_bSpecialTypeTable[wCmpUser] == true) {
						if (m_GameLogic.GetCardType(m_bUserCardData.get(wUser), (byte) 13,
								m_GameLogic.btCardSpecialData) > m_GameLogic.GetCardType(m_bUserCardData.get(wCmpUser),
										(byte) 13, m_GameLogic.btCardSpecialData)) {
							aPlayerWinDao += cbSpecialUserDaoShu;
							sssRound.m_bSpecialCompareResult[wUser] += cbSpecialUserDaoShu;
						} else if (m_GameLogic.GetCardType(m_bUserCardData.get(wUser), (byte) 13,
								m_GameLogic.btCardSpecialData) < m_GameLogic.GetCardType(m_bUserCardData.get(wCmpUser),
										(byte) 13, m_GameLogic.btCardSpecialData)) {
							aPlayerWinDao -= cbSpecialCmpUserDaoShu;
							sssRound.m_bSpecialCompareResult[wUser] -= cbSpecialCmpUserDaoShu;
						}
					} else if (m_bSpecialTypeTable[wUser] == false && m_bSpecialTypeTable[wCmpUser] == true) {
						aPlayerWinDao -= cbSpecialCmpUserDaoShu;
						sssRound.m_bSpecialCompareResult[wUser] -= cbSpecialCmpUserDaoShu;
					}
					// //马牌输赢加倍
					// byte mapai = sssRound.getMapai();
					// if (mapai > 0) {
					// byte[] cardlist = m_bUserCardData.get(wUser);
					// byte[] other_cardlist = m_bUserCardData.get(wCmpUser);
					// //我有马牌
					// int mapai_index = ArrayUtils.indexOf(cardlist, mapai);
					// //对方有马牌
					// int other_mapai_index = ArrayUtils.indexOf(other_cardlist, mapai);
					// if (mapai_index > -1 || other_mapai_index> -1) {
					// aPlayerWinDao *= 2;
					// }
					// }
					lWinDaoShu += aPlayerWinDao;
				}
			}
			// 总分
			sssRound.m_lGameScore[wUser] += lWinDaoShu * sssRound.m_lCellScore;
			sssRound.m_bToltalWinDaoShu[wUser] += lWinDaoShu;
			if (sssRound.getRoomFeeType() != 2 && sssRound.getRoomFee() > 0) {// 扣去房费
				sendSssCalculateFangka((short) sssRound.getRoomFee(), sssRound.getRoomFeeType(),
						sssRound.getPlayerIds());
				sssRound.setRoomFee(0);
			}
			logger.info("分数={}", sssRound.m_lGameScore[wUser]);
		}
		/// <下面判断是否全垒打在加减分
		if (GAME_PLAYER >= 3) {
			int allKingScore = 12;
			for (int i = 0; i < GAME_PLAYER; i++) {
				if (WinNum[i] == GAME_PLAYER - 1) {
					for (int j = 0; j < GAME_PLAYER; j++) {
						logger.info("玩家={}进入全垒打,target={}", i, j);
						if (i == j) {
							logger.info("分数={}", sssRound.m_lGameScore[i]);
							sssRound.m_lGameScore[j] += allKingScore * (GAME_PLAYER - 1) * sssRound.m_lCellScore;
							sssRound.m_bToltalWinDaoShu[j] += allKingScore * (GAME_PLAYER - 1);
							sssRound.m_bThreeKillResult[j] = allKingScore * (GAME_PLAYER - 1);
						} else {
							sssRound.m_lGameScore[j] -= 12 * sssRound.m_lCellScore;
							sssRound.m_bToltalWinDaoShu[j] -= allKingScore;
							sssRound.m_bThreeKillResult[j] = -allKingScore;
						}
					}
					break;
				}
			}
		}

		// if(m_pGameServiceOption->wServerType==GAME_GENRE_GOLD)
		// {
		// long lMoreScore=0;
		// long lTrueScore=0;
		// bool bdo=false;
		// IServerUserItem * pIServerUserItem=NULL;
		// //输的人输的分数大于自己的分数，只输自己的分数
		// bool bMoreScore[GAME_PLAYER];
		// ZeroMemory(bMoreScore,sizeof(bMoreScore));
		// do
		// {
		// lMoreScore=0;
		// lTrueScore=0;
		// bdo=false;
		// for(int i=0;i<GAME_PLAYER;i++)
		// {
		// if(m_bPlayer[i]&&m_bUserLeft[i]==false)
		// {
		// if(m_lGameScore[i]<0)
		// {
		// long lUserScore=0;
		// pIServerUserItem=m_pITableFrame->GetTableUserItem(i);
		// if(pIServerUserItem)
		// lUserScore=pIServerUserItem->GetUserScore();
		// long lTemp=lUserScore+m_lGameScore[i];
		// if(lTemp<0)
		// {
		// bMoreScore[i]=true;
		// bdo=true;
		// lTrueScore=-lUserScore;
		// lMoreScore=m_lGameScore[i];
		// m_lGameScore[i]=-lUserScore;
		// break;
		// }
		// }
		//
		// }
		// }
		// if(bdo)
		// {
		// int nLeaveCount=0;
		// //有多少玩家
		// for(int i=0;i<GAME_PLAYER;i++)
		// {
		// if(m_bPlayer[i]&&bMoreScore[i]==false)
		// {
		// nLeaveCount++;
		// }
		// }
		// //其他人按照输分超的人输的分数和输分超的人自己的分数的比例调整
		// int LeaveNum=0;
		// for(int i=0;i<GAME_PLAYER;i++)
		// {
		// if(m_bPlayer[i]&&bMoreScore[i]==false)
		// {
		// LeaveNum+=m_lGameScore[i]*lTrueScore%lMoreScore;
		// m_lGameScore[i]=m_lGameScore[i]*lTrueScore/lMoreScore;
		// }
		// }
		// if(LeaveNum!=0)
		// {
		// long lTemp=LeaveNum%nLeaveCount;
		// long lEveryTemp=LeaveNum/nLeaveCount;
		// int nCount=0;
		// int nrand=rand()%nLeaveCount+1;
		// for(int i=0;i<GAME_PLAYER;i++)
		// {
		// if(m_bPlayer[i]&&bMoreScore[i]==false)
		// {
		// nCount++;
		// if(nrand==nCount)
		// {
		// m_lGameScore[i]+=lEveryTemp;
		// m_lGameScore[i]+=lTemp;
		// }
		// else
		// {
		// m_lGameScore[i]+=lEveryTemp;
		// }
		// }
		// }
		// }
		// }
		//
		// } while (bdo);
		//
		// //赢的人赢的分数大于自己的分数只赢自己的分数
		// ZeroMemory(bMoreScore,sizeof(bMoreScore));
		// do
		// {
		// lMoreScore=0;
		// lTrueScore=0;
		// bdo=false;
		// for(int i=0;i<GAME_PLAYER;i++)
		// {
		// if(m_bPlayer[i]&&m_bUserLeft[i]==false)
		// {
		// if(m_lGameScore[i]>0)
		// {
		// long lUserScore=0;
		// pIServerUserItem=m_pITableFrame->GetTableUserItem(i);
		// if(pIServerUserItem)
		// lUserScore=pIServerUserItem->GetUserScore();
		// long lTemp=m_lGameScore[i]-lUserScore;
		// if(lTemp>0)
		// {
		// bMoreScore[i]=true;
		// bdo=true;
		// lTrueScore=lUserScore;
		// lMoreScore=m_lGameScore[i];
		// m_lGameScore[i]=lUserScore;
		// break;
		// }
		// }
		//
		// }
		// }
		//
		// if(bdo)
		// {
		// int nLeaveCount=0;
		// //有多少玩家
		// for(int i=0;i<GAME_PLAYER;i++)
		// {
		// if(m_bPlayer[i]&&bMoreScore[i]==false)
		// {
		// nLeaveCount++;
		// }
		// }
		// //其他人按照赢分超的人赢的分数和赢分超的人自己的分数的比例调整
		// int LeaveNum=0;
		// for(int i=0;i<GAME_PLAYER;i++)
		// {
		// if(m_bPlayer[i]&&bMoreScore[i]==false)
		// {
		// LeaveNum+=m_lGameScore[i]*lTrueScore%lMoreScore;
		// m_lGameScore[i]=m_lGameScore[i]*lTrueScore/lMoreScore;
		// }
		// }
		// if(LeaveNum!=0)
		// {
		// long lTemp=LeaveNum%nLeaveCount;
		// long lEveryTemp=LeaveNum/nLeaveCount;
		// int nCount=0;
		// int nrand=rand()%nLeaveCount+1;
		// for(int i=0;i<GAME_PLAYER;i++)
		// {
		// if(m_bPlayer[i]&&bMoreScore[i]==false)
		// {
		// nCount++;
		// if(nrand==nCount)
		// {
		// m_lGameScore[i]+=lEveryTemp;
		// m_lGameScore[i]+=lTemp;
		// }
		// else
		// {
		// m_lGameScore[i]+=lEveryTemp;
		// }
		// }
		// }
		// }
		// }
		//
		// } while (bdo);
		// }
		// 机器人分数统计
		// m_WinnerScore = 0;
		// bool bUser = false;
		// for(int i=0;i<GAME_PLAYER;i++)
		// {
		// IServerUserItem* pIServerUserItem=m_pITableFrame->GetTableUserItem(i);
		// if(pIServerUserItem!=NULL && !pIServerUserItem->IsAndroidUser() &&
		// m_bPlayer[i])
		// {
		// bUser = true;
		// break;
		// }
		// }
		// if(bUser)
		// {
		// for(int j=0;j<GAME_PLAYER;j++)
		// {
		// IServerUserItem* pIServerUserItem=m_pITableFrame->GetTableUserItem(j);
		// if(pIServerUserItem!=NULL && pIServerUserItem->IsAndroidUser() &&
		// m_bPlayer[j])
		// {
		// m_WinnerScore+=m_lGameScore[j];
		// }
		// }
		// }
		// logger.info(" 该局机器人输赢 = {}",sssRound.m_WinnerScore);
		for (byte i = 0; i < GAME_PLAYER; i++) {
			logger.info(" 玩家={}得分 ={}", i, sssRound.m_lGameScore[i]);
		}
		if (sssRound.getIs_jindou() == 1) {// 金豆房间结算
			int score[] = ArrayUtility.long2Int(sssRound.m_lGameScore);
			sendSssCalculatteHallServer(score, sssRound.getPlayerIds(), sssRound.getJindouDi(), sssRound.getRoomUuid(),
					sssRound.getCurrentRound(), RoomType.SssRoom);
		}
	}

	public void sendSssJindouRoomChargeFee(int jindouDi, List<Integer> players) {
		logger.info("sss jindou room charge room fee");
		String rsLanHost = RoomServer.getInstance().getServerConfig().getHallLanhost();

		int rsLanPort = RoomServer.getInstance().getServerConfig().getHallLanport();
		boolean reconnectRet = RoomServer.getInstance().getLanClientManager().connect(rsLanHost, rsLanPort);
		if(reconnectRet) {
			RoomServer.getInstance().getLanClientManager().sendCalculateJindouFee(rsLanHost,rsLanPort,jindouDi,players);
		}else {
			logger.error("connect 2 roomServer error!rsLanHost={},rsLanPort={}", rsLanHost, rsLanPort);
		}
		
	}

	public void sendSssCalculatteHallServer(int score[], List<Integer> players, int gold_coin, String roomid,
			short roundcount, int room_type) {
		logger.info("sss calculate jindou");
		String rsLanHost = RoomServer.getInstance().getServerConfig().getHallLanhost();

		int rsLanPort = RoomServer.getInstance().getServerConfig().getHallLanport();
		boolean reconnectRet = RoomServer.getInstance().getLanClientManager().connect(rsLanHost, rsLanPort);
		if (reconnectRet) {
			RoomServer.getInstance().getLanClientManager().sendCalculateHallServer(rsLanHost, rsLanPort, score, players,
					gold_coin, roomid, roundcount, room_type);
		} else {
			logger.error("connect 2 roomServer error!rsLanHost={},rsLanPort={}", rsLanHost, rsLanPort);
		}
	}

	public void allRoundOver(SssRound sssRound) {
		List<Integer> playerIds = sssRound.getPlayerIds();
		if (sssRound.getRoomFeeType() == 2 && sssRound.getRoomFee() > 0) {// 扣去房费
			List<Integer> play = new ArrayList<Integer>();
			short score[] = ArrayUtility.int2Short(sssRound.m_bToltaSocre);
			int maxPlayerIndex = 0;
			for (int i = 0; i < score.length; i++) {
				if (score[i] == maxPlayerIndex)
					play.add(playerIds.get(i));
				if (score[i] > maxPlayerIndex) {
					play.clear();
					play.add(playerIds.get(i));
					maxPlayerIndex = score[i];
				}
				
			}
			sendSssCalculateFangka((short) sssRound.getRoomFee(), sssRound.getRoomFeeType(), play);
		}
		for (Integer aPlayerId : playerIds) {
			SssDataManager.getInstance().removePlayer(aPlayerId);
		}
	}

	private boolean checkMaPai(SssRound sssRound, int wUser, int wCmpUser) {
		byte mapai = sssRound.getMapai();
		if (mapai > 0) {
			List<byte[]> m_bUserCardData = sssRound.getCards();
			byte[] cardlist = m_bUserCardData.get(wUser);
			byte[] other_cardlist = m_bUserCardData.get(wCmpUser);
			// 我有马牌
			int mapai_index = ArrayUtils.indexOf(cardlist, mapai);
			// 对方有马牌
			int other_mapai_index = ArrayUtils.indexOf(other_cardlist, mapai);
			if (mapai_index > -1 || other_mapai_index > -1) {
				return true;
			}
		}
		return false;
	}

	private List<byte[]> randCards(int playerCount,byte[] addColor) {
		RandomDispatcher<Byte> rd = new RandomDispatcher<>();
		//第一副牌
		for (short i = 0; i < SssConstants.m_cbCardListData.length; i++) {
			rd.put(1, SssConstants.m_cbCardListData[i]);
		}
		//加的颜色
		if(addColor != null) {
			for (byte b : addColor) {
				byte[] aColorCards = SssConstants.m_cbCardListDataColor[b];
				for (byte c : aColorCards) {
					rd.put(1, c);
				}
			}
		}
		List<byte[]> cards = new ArrayList<>();
		for (int i = 0; i < playerCount; i++) {
			byte[] aPlayerCards = new byte[13];
			for (int j = 0; j < 13; j++) {
				aPlayerCards[j] = rd.randomRemove();
			}
			cards.add(aPlayerCards);
		}
		
//		// 混乱准备
//		byte[] cbCardBuffer = new byte[SssConstants.m_cbCardListData.length];
//		byte cbBufferCount = (byte) cbCardBuffer.length;
//		byte[] cbCardData = Arrays.copyOf(SssConstants.m_cbCardListData, SssConstants.m_cbCardListData.length);
//		// 混乱扑克
//		byte bRandCount = 0, bPosition = 0;
//		do {
//			bPosition = (byte) (RandomUtils.nextInt(0, Integer.MAX_VALUE)
//					% (SssConstants.m_cbCardListData.length - bRandCount));
//			cbCardBuffer[bRandCount++] = cbCardData[bPosition];
//			cbCardData[bPosition] = cbCardData[SssConstants.m_cbCardListData.length - bRandCount];
//		} while (bRandCount < cbBufferCount);
//		//
//		List<byte[]> cards = new ArrayList<>();
//		for (int i = 0; i < playerCount; i++) {
//			byte[] aPlayerCards = Arrays.copyOfRange(cbCardBuffer, 13 * i, 13 * (i + 1));
//			cards.add(aPlayerCards);
//		}
		return cards;
	}

	private List<byte[]> randCardsSpecial(int playerCount) {
		// 特殊牌型 同花十三水//
		byte[] test1 = new byte[] { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D };
		// 特殊牌型 十三水//
		byte[] test2 = new byte[] { 0x01, 0x12, 0x23, 0x34, 0x25, 0x26, 0x07, 0x28, 0x29, 0x2A, 0x3B, 0x2C, 0x1D };
		// 特殊牌型 十二皇族//
		byte[] test3 = new byte[] { 0x01, 0x0B, 0x1B, 0x2B, 0x1C, 0x2C, 0x3C, 0x0D, 0x1D, 0x2D, 0x31, 0x21, 0x3D };
		// 特殊牌型 三同花顺//
		byte[] test4 = new byte[] { 0x12, 0x13, 0x14, 0x24, 0x25, 0x26, 0x27, 0x28, 0x07, 0x08, 0x09, 0x0A, 0x0B };
		// 特殊牌型 三炸弹//
		byte[] test5 = new byte[] { 0x01, 0x11, 0x21, 0x31, 0x08, 0x18, 0x28, 0x38, 0x0C, 0x1C, 0x2C, 0x3C, 0x0B };
		// 特殊牌型 全大//
		byte[] test6 = new byte[] { 0x09, 0x29, 0x1A, 0x1B, 0x1C, 0x2C, 0x2B, 0x3B, 0x1D, 0x19, 0x2D, 0x3C, 0x3A };
		// 特殊牌型 全小//
		byte[] test7 = new byte[] { 0x14, 0x16, 0x22, 0x23, 0x25, 0x35, 0x27, 0x37, 0x04, 0x34, 0x13, 0x26, 0x33 };
		// 特殊牌型 凑一色//
		byte[] test8 = new byte[] { 0x21, 0x22, 0x23, 0x04, 0x25, 0x06, 0x27, 0x28, 0x29, 0x03, 0x2B, 0x05, 0x02 };
		// 特殊牌型 四套三条//
		byte[] test9 = new byte[] { 0x03, 0x13, 0x23, 0x14, 0x24, 0x34, 0x1A, 0x2A, 0x3A, 0x1D, 0x2D, 0x3D, 0x28 };
		// 特殊牌型 五对三条//
		byte[] test10 = new byte[] { 0x03, 0x13, 0x14, 0x24, 0x36, 0x06, 0x1A, 0x2A, 0x2B, 0x3B, 0x08, 0x38, 0x28 };
		// 特殊牌型 六对半//
		byte[] test11 = new byte[] { 0x03, 0x13, 0x14, 0x24, 0x36, 0x06, 0x1A, 0x2A, 0x2B, 0x3B, 0x2D, 0x3D, 0x28 };
		// 特殊牌型 三同花//
		byte[] test12 = new byte[] { 0x12, 0x14, 0x15, 0x25, 0x27, 0x29, 0x28, 0x2A, 0x09, 0x0C, 0x03, 0x0D, 0x06 };
		// 特殊牌型 三顺子//
		byte[] test13 = new byte[] { 0x12, 0x33, 0x34, 0x25, 0x06, 0x17, 0x28, 0x19, 0x08, 0x39, 0x1A, 0x2B, 0x2C };
		// do
		List<byte[]> randSpecialCards = new ArrayList<>();
		randSpecialCards.add(test1);
		randSpecialCards.add(test2);
		randSpecialCards.add(test3);
		randSpecialCards.add(test4);
		randSpecialCards.add(test5);
		randSpecialCards.add(test6);
		randSpecialCards.add(test7);
		randSpecialCards.add(test8);
		randSpecialCards.add(test9);
		randSpecialCards.add(test10);
		randSpecialCards.add(test11);
		randSpecialCards.add(test12);
		randSpecialCards.add(test13);
		byte[] randCards = randSpecialCards.get(RandomUtils.nextInt(0, randSpecialCards.size()));
		RandomDispatcher<Byte> rd = new RandomDispatcher<>();
		for (short i = 0; i < SssConstants.m_cbCardListData.length; i++) {
			byte aCardVal = SssConstants.m_cbCardListData[i];
			if (ArrayUtils.contains(randCards, aCardVal)) {
				continue;
			}
			rd.put(1, aCardVal);
		}
		List<byte[]> cards = new ArrayList<>();
		cards.add(randCards);
		for (int i = 0; i < playerCount - 1; i++) {
			byte[] aPlayerCards = new byte[13];
			for (int j = 0; j < 13; j++) {
				aPlayerCards[j] = rd.randomRemove();
			}
			cards.add(aPlayerCards);
		}
		return cards;
	}

	/**
	 * 十三水结算
	 * 
	 * @param ddzRound
	 */
	public void claculateResult(SssRound sssRound) {
		Map<Integer, byte[]> peiPaiFinishMap = sssRound.getPeiPaiFinish();
		int playerCount = sssRound.getPlayerIds().size();
		ComputeChout(sssRound);
		ComputeResult(sssRound);
		// send
		PushSssCalculate pushMsg = new PushSssCalculate();
		pushMsg.card_all = new SssCardArray[playerCount];
		for (int i = 0; i < playerCount; i++) {
			int playerId = sssRound.getPlayerIds().get(i);
			pushMsg.card_all[i] = new SssCardArray();
			pushMsg.card_all[i].pai = peiPaiFinishMap.get(playerId);
		}
		pushMsg.m_bCompareResult = new IntList[sssRound.m_bCompareResult.length];
		for (int i = 0; i < sssRound.m_bCompareResult.length; i++) {
			pushMsg.m_bCompareResult[i] = new IntList();
			pushMsg.m_bCompareResult[i].pai = sssRound.m_bCompareResult[i];
		}
		pushMsg.m_bShootState = new IntList[sssRound.m_nXShoot];
		for (int i = 0; i < sssRound.m_nXShoot; i++) {
			pushMsg.m_bShootState[i] = new IntList();
			pushMsg.m_bShootState[i].pai = sssRound.m_bShootState[i];
		}
		pushMsg.m_bThreeKillResult = sssRound.m_bThreeKillResult;
		pushMsg.m_bToltalWinDaoShu = sssRound.m_bToltalWinDaoShu;
		pushMsg.m_bCompareDouble = sssRound.m_bCompareDouble;
		pushMsg.m_bSpecialCompareResult = sssRound.m_bSpecialCompareResult;
		pushMsg.use_special = sssRound.m_bSpecialTypeTable;
		pushMsg.m_lGameScore = ArrayUtility.long2Int(sssRound.m_lGameScore);
		for (int i = 0; i < playerCount; i++) {
			sssRound.m_bToltaSocre[i] += sssRound.m_lGameScore[i];
		}
		// push
		logger.info("use_special={}", pushMsg.use_special);
		for (Integer aPlayerId : sssRound.getPlayerIds()) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
	}

	public void nextRound(SssRound ddzRound) {
		// 数据重置
		ddzRound.getCards().clear();
		ddzRound.getPeiPaiFinish().clear();
		// 重新生成牌
		List<byte[]> cardListAll = randCards(ddzRound.getPlayerIds().size(),ddzRound.getAdd_color());
		ddzRound.setCards(cardListAll);
		ddzRound.setCurrentRound((short) (ddzRound.getCurrentRound() + 1));
		//
		Arrays.fill(ddzRound.getSssAlgorithm().btCardSpecialData, PrimitiveTypeConstants.BYTE_ZERO);
		Arrays.fill(ddzRound.m_bSpecialTypeTable, false);
		ArrayUtility.fill(ddzRound.m_bSegmentCard, PrimitiveTypeConstants.BYTE_ZERO);
		ArrayUtility.fill(ddzRound.cbResult, PrimitiveTypeConstants.BYTE_ZERO);
		Arrays.fill(ddzRound.cbSpecialResult, PrimitiveTypeConstants.BYTE_ZERO);
		//
		ArrayUtility.fill(ddzRound.m_bCompareResult, 0);
		ArrayUtility.fill(ddzRound.m_bShootState, 0);
		Arrays.fill(ddzRound.m_bThreeKillResult, 0);
		Arrays.fill(ddzRound.m_bToltalWinDaoShu, 0);
		Arrays.fill(ddzRound.m_bCompareDouble, 0);
		Arrays.fill(ddzRound.m_bSpecialCompareResult, 0);
		Arrays.fill(ddzRound.m_lGameScore, 0);
		ddzRound.m_nXShoot = PrimitiveTypeConstants.BYTE_ZERO;
		ddzRound.getNextRoundReady().clear();
		// 发送消息
		List<Integer> playerIds = ddzRound.getPlayerIds();
		PushSssNextRound pushMsg = new PushSssNextRound();
		int playerIndex = 0;
		for (Integer aPlayerId : playerIds) {
			// 推送
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				byte[] cardList = ddzRound.getCards().get(playerIndex++);
				pushMsg.my_cards = cardList;
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
		if(ddzRound.getIs_jindou()==1) {
			sendSssJindouRoomChargeFee(ddzRound.getJindouDi(),ddzRound.getPlayerIds());
		}
	}

	public static void main(String[] args) {
		// SssLogic sssLogic = new SssLogic();
		// SssRound sssRound = new SssRound();
		// List<byte[]> cardList = sssLogic.randCards(4);
		// sssRound.setCards(cardList);
		// logger.info("sssRound ={}", sssRound);
		byte[][][] m_bSegmentCard = new byte[4][3][SssConstants.MAX_COUNT];
		m_bSegmentCard[0][2][0] = (byte) 1;
		logger.info("a={}", m_bSegmentCard);
		SssAlgorithm sssAlgorithm = new SssAlgorithm();
		byte[] bCardData = new byte[] { 61, 45, 59, 43, 42, 26, 54, 6, 36, 20, 19, 3, 3 };
		byte bCardCount = 13;

		byte cardType = sssAlgorithm.GetCardType(bCardData, bCardCount, sssAlgorithm.btCardSpecialData);
		System.out.println(cardType);
	}

}
